home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_std / integer.e < prev    next >
Text File  |  1998-12-22  |  7KB  |  347 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. expanded class INTEGER
  13. --
  14. -- Note: An Eiffel INTEGER is mapped as a C int or as a Java int.
  15. --
  16.  
  17. inherit
  18.    INTEGER_REF
  19.       redefine
  20.      infix "+", infix "-", infix "*", infix "/", infix "\\", 
  21.      infix "//", infix "^", infix "<", infix "<=", infix ">",
  22.      infix ">=", compare, prefix "-", prefix "+", hash_code, 
  23.      one, zero, out_in_tagged_out_memory, fill_tagged_out_memory
  24.       end;
  25.  
  26. feature 
  27.    
  28.    one: INTEGER is 1;
  29.  
  30.    zero: INTEGER is 0;
  31.  
  32.    infix "+"(other: INTEGER): INTEGER is
  33.       external "SmallEiffel"
  34.       end;
  35.    
  36.    infix "-" (other: INTEGER): INTEGER is
  37.       external "SmallEiffel"
  38.       end;
  39.  
  40.    infix "*" (other: INTEGER): INTEGER is
  41.       external "SmallEiffel"
  42.       end;
  43.  
  44.    infix "/" (other: INTEGER): DOUBLE is
  45.       external "SmallEiffel"
  46.       end;
  47.  
  48.    infix "//" (other: INTEGER): INTEGER is
  49.       external "SmallEiffel"
  50.       end;
  51.  
  52.    infix "\\" (other: INTEGER): INTEGER is
  53.       external "SmallEiffel"
  54.       end;
  55.    
  56.    infix "^" (exp: INTEGER): INTEGER is
  57.       do
  58.      if exp = 0 then
  59.         Result := 1;
  60.      elseif exp \\ 2 = 0 then
  61.         Result := (Current * Current) ^ (exp // 2);
  62.      else
  63.         Result := Current * (Current ^ (exp - 1));
  64.      end;
  65.       end;
  66.    
  67.    abs: INTEGER is
  68.      -- Absolute value of `Current'.
  69.       do
  70.      if Current < 0 then
  71.         Result := -Current;
  72.      else
  73.         Result := Current;
  74.      end;
  75.       end;
  76.          
  77.    infix "<" (other: INTEGER): BOOLEAN is
  78.      -- Is 'Current' strictly less than 'other'?
  79.       external "SmallEiffel"
  80.       end;
  81.  
  82.    infix "<=" (other: INTEGER): BOOLEAN is
  83.      -- Is 'Current' less or equal 'other'?
  84.       external "SmallEiffel"
  85.       end;
  86.  
  87.    infix ">" (other: INTEGER): BOOLEAN is
  88.      -- Is 'Current' strictly greater than 'other'?
  89.       external "SmallEiffel"
  90.       end;
  91.  
  92.    infix ">=" (other: INTEGER): BOOLEAN is
  93.      -- Is 'Current' greater or equal than 'other'?
  94.       external "SmallEiffel"
  95.       end;
  96.  
  97.    prefix "+": INTEGER is
  98.       do
  99.      Result := Current
  100.       end;
  101.  
  102.    prefix "-": INTEGER is
  103.       external "SmallEiffel"
  104.       end;
  105.    
  106.    compare(other: INTEGER): INTEGER is
  107.       do
  108.      Result := Current - other
  109.       end;
  110.  
  111.    odd: BOOLEAN is
  112.      -- Is odd ?
  113.       do
  114.      Result := (Current \\ 2) = 1;
  115.       end;
  116.  
  117.    even: BOOLEAN is
  118.      -- Is even ?
  119.       do
  120.      Result := (Current \\ 2) = 0;
  121.       end;
  122.  
  123.    sqrt: DOUBLE is
  124.      -- Compute the square routine.
  125.       do
  126.      Result := to_double.sqrt;
  127.       end;
  128.  
  129.    log: DOUBLE is
  130.      -- (ANSI C log).
  131.       do
  132.      Result := to_double.log;
  133.       end;
  134.  
  135.    gcd(other: INTEGER): INTEGER is
  136.      -- Great Common Divisor of `Current' and `other'.
  137.       require
  138.      Current >= 0;
  139.          other >= 0;
  140.       do
  141.      if other = 0 then 
  142.         Result := Current
  143.          else
  144.             Result := other.gcd(Current \\ other);
  145.          end;    
  146.       ensure
  147.          Result = other.gcd(Current);
  148.       end;
  149.    
  150. feature -- Conversions :
  151.    
  152.    to_real: REAL is
  153.       do
  154.      Result := Current;
  155.       end;
  156.    
  157.    to_double: DOUBLE is
  158.       do
  159.      Result := Current;
  160.       end;
  161.    
  162.    to_string: STRING is
  163.      -- Convert the INTEGER into a new allocated STRING. 
  164.      -- Note: see also `append_in' to save memory.
  165.       do
  166.      !!Result.make(0);
  167.      append_in(Result);
  168.       end; 
  169.  
  170.    to_boolean: BOOLEAN is
  171.      -- Return false for 0, otherwise true.
  172.       do
  173.      Result := Current /= 0;
  174.       ensure 
  175.      Result = (Current /= 0)
  176.       end;
  177.  
  178.    to_bit: BIT Integer_bits is
  179.      -- Portable BIT_N conversion.
  180.       external "SmallEiffel"
  181.       end;
  182.  
  183.    append_in(str: STRING) is
  184.      -- Append the equivalent of `to_string' at the end of 
  185.      -- `str'. Thus you can save memory because no other
  186.      -- STRING is allocate for the job.
  187.       require
  188.      str /= Void;
  189.       local
  190.      val, i: INTEGER;
  191.       do
  192.      if Current = 0 then
  193.         str.extend('0');
  194.      else
  195.         if Current > 0 then
  196.            from
  197.           i := str.count + 1;
  198.           val := Current;
  199.            until
  200.           val = 0
  201.            loop
  202.           str.extend((val \\ 10).digit);
  203.           val := val // 10;
  204.            end;
  205.         else
  206.            str.extend('-');
  207.            from
  208.           i := str.count + 1;
  209.           val := Current;
  210.            until
  211.           val = 0
  212.            loop
  213.           str.extend((-(val \\ 10)).digit);
  214.           val := val // 10;
  215.            end;
  216.         end;
  217.         from  
  218.            val := str.count;
  219.         until
  220.            i >= val
  221.         loop
  222.            str.swap(i,val);
  223.            val := val - 1;
  224.            i := i + 1;
  225.         end;
  226.      end;
  227.       end; 
  228.    
  229.    to_string_format(s: INTEGER): STRING is
  230.      -- Same as `to_string' but the result is on `s' character and the 
  231.      -- number is right aligned. 
  232.      -- Note: see `append_in_format' to save memory.
  233.       require
  234.      to_string.count <= s;
  235.       do
  236.      from  
  237.         tmp_string.clear;
  238.         append_in(tmp_string);
  239.      until
  240.         tmp_string.count >= s
  241.      loop
  242.         tmp_string.add_first(' ');
  243.      end;
  244.      Result := tmp_string.twin;
  245.       ensure
  246.      Result.count = s;
  247.       end; 
  248.  
  249.    append_in_format(str: STRING; s: INTEGER) is
  250.      -- Append the equivalent of `to_string_format' at the end of 
  251.      -- `str'. Thus you can save memory because no other
  252.      -- STRING is allocate for the job.
  253.       do
  254.      from
  255.         tmp_string.clear;
  256.         append_in(tmp_string);
  257.      until
  258.         tmp_string.count >= s
  259.      loop
  260.         tmp_string.add_first(' ');
  261.      end;
  262.      str.append(tmp_string);
  263.       ensure
  264.      str.count >= (old str.count) + s;
  265.       end;
  266.    
  267.    digit: CHARACTER is
  268.      -- Gives the corresponding CHARACTER for range 0..9.
  269.       require
  270.      in_range(0,9)
  271.       do
  272.      Result := (Current + ('0').code).to_character;
  273.       ensure
  274.      ("0123456789").has(Result);
  275.      Result.value = Current
  276.       end;
  277.       
  278.    hexadecimal_digit: CHARACTER is
  279.      -- Gives the corresponding CHARACTER for range 0..15.
  280.       require
  281.      in_range(0,15)
  282.       do
  283.      if Current <= 9 then
  284.         Result := digit;
  285.      else
  286.         Result := (('A').code + (Current - 10)).to_character;
  287.      end;
  288.       ensure
  289.      ("0123456789ABCDEF").has(Result)
  290.       end;
  291.       
  292.    to_character: CHARACTER is
  293.      -- Return the coresponding ASCII character.
  294.       require
  295.      Current >= 0;
  296.       external "SmallEiffel"
  297.       end;
  298.    
  299.    to_octal: INTEGER is
  300.      -- Gives the octal value.
  301.       do
  302.      if Current = 0 then
  303.      elseif Current < 0 then
  304.         Result := -((-Current).to_octal);
  305.      else
  306.         from  
  307.            tmp_string.clear;
  308.            Result := Current;
  309.         until
  310.            Result = 0
  311.         loop
  312.            tmp_string.extend((Result \\ 8).digit);
  313.            Result := Result // 8;
  314.         end;        
  315.         tmp_string.reverse;
  316.         Result := tmp_string.to_integer;
  317.      end;
  318.       end;
  319.    
  320. feature -- Object Printing :
  321.  
  322.    out_in_tagged_out_memory, fill_tagged_out_memory is
  323.       do
  324.      Current.append_in(tagged_out_memory);
  325.       end;
  326.  
  327. feature -- Hashing :
  328.    
  329.    hash_code: INTEGER is
  330.       do
  331.      if Current < 0 then
  332.         Result := -(Current + 1);
  333.      else
  334.         Result := Current;
  335.      end
  336.       end;
  337.     
  338. feature {NONE}
  339.    
  340.    tmp_string: STRING is 
  341.       once
  342.      !!Result.make(128);
  343.       end;
  344.      
  345. end -- INTEGER
  346.  
  347.